#!/usr/local/bin/tea

//system.stdout("Content-type: text/plain\n\n");
require.paths.unshift("./code");

require('http');
// HACK

var server=Array.prototype.globalStore;
if (!server) {
	var LNServer=require('server/Server').LNServer;
	server=new LNServer();
	Array.prototype.globalStore=server;
}
if (system.env.TERM || system.args.length>1) {
	server.console_work();
} else {
	try {
		server.http_work(request,response);
	} catch(e) {
		system.stdout.write("Content-type: text/plain\n\nCORE ERROR\n"+e+"\n");
		system.stdout.write("DEBUG: typeof e="+(typeof e)+"\n");
		if (typeof e==="object") {
			if (e.constructor==TypeError) {
					system.stdout.write("message="+e.message+"\n");
					system.stdout.write("fileName="+e.fileName+"\n");
					system.stdout.write("lineNumber="+e.lineNumber+"\n");
					system.stdout.write("stack="+e.stack+"\n");
			} else {
				for (var k in e) {
					system.stdout.write(k+"="+e[k]+"\n");
				}
			}
		}
	}
}
